<template>
  <el-dialog
      model-value="compandingDeploymentDialogVisible"
      title="副本数"
      width="30%"
      @close="handleClose"
  >

    <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
    >

      <el-form-item label="副本数" prop="replicas">
       <el-input-number v-model="form.replicas" :min="1" label="副本数"></el-input-number>
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>

import {defineEmits, defineProps, ref, watch} from "vue";
import requestUtil from "@/util/request";
import {ElMessage} from 'element-plus'

const props = defineProps({
      resource_name: {
        type: String,
        required: true,
        default: '',
      },
      resource_namespace: {
        type: String,
        required: true,
        default: '',
      },
      compandingDeploymentDialogVisible: {
        type: Boolean,
        default: false,
        required: true
      }
    }
)


const form = ref({
  replicas: "",
})


const rules = ref({
  namespce: [
    {required: true, message: '请输入副本数', trigger: "blur"},
  ],
})

const formRef = ref(null)

const emits = defineEmits(['update:modelValue', 'initUserList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const tempQueryForm = {...form.value, name: props.resource_name, namespace: props.resource_namespace };
      let result = await requestUtil.put("workload/deployment_api", tempQueryForm);
      let data = result.data;
      if (data.code === 200) {
        ElMessage.success(data.msg)
        formRef.value.resetFields();
        emits("initUserList")
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log("fail")
    }
  })
}

</script>

<style scoped>

</style>
